package com.keta.foundation.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.keta.foundation.dao.MonitorDAO;
import com.keta.foundation.entity.Monitor;
import com.keta.foundation.service.MonitorService;
import com.keta.foundation.util.dwz.Page;
import com.keta.foundation.util.dwz.PageUtil;

@Service
@Transactional
public class MonitorServiceImpl implements MonitorService {

	@Autowired
	private MonitorDAO monitorDAO;
	
	@Override
	public Monitor get(Long id) {
		return monitorDAO.findOne(id);
	}
	

	@Override
	public long getMonitorCount() {
		return monitorDAO.count();
	}

	@Override
	public void saveOrUpdate(Monitor monitor) {
		monitorDAO.save(monitor);
	}
	
	@Override
	public List<Monitor> getAll() {
		return monitorDAO.findAll();
	}

	@Override
	public List<Monitor> getAll(Page page) {
		org.springframework.data.domain.Page<Monitor> springPage = monitorDAO.findAll(PageUtil.createPageable(page));
		page.setTotalPage(springPage.getTotalPages());
		page.setTotalCount(springPage.getTotalElements());
		return springPage.getContent();
	}

	@Override
	public List<Monitor> getAll(Specification<Monitor> specification, Page page) {
		org.springframework.data.domain.Page<Monitor> springPage = monitorDAO.findAll(specification,
				PageUtil.createPageable(page));
		page.setTotalPage(springPage.getTotalPages());
		page.setTotalCount(springPage.getTotalElements());
		return springPage.getContent();
	}

	@Override
	public void delete(Long id) {
		monitorDAO.delete(id);
	}
	
	@Override
	public void delete(Monitor monitor) {
		monitorDAO.delete(monitor);
	}

}
